<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="80px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="资源名称" prop="metaName">
            <el-input v-model="formData.metaName" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="设定天数" prop="days">
            <el-select
              v-model="formData.days"
              clearable
              placeholder="请选择天数，系统会根据天数进行定期的清理工作"
              @change="handleOptionChange(formData.days)"
              size="large"
              placement="bottom"
            >
              <el-option
                v-for="item in options"
                :key="item.label"
                :label="item.value"
                :value="item.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <!-- <span style="display: block; margin-bottom: 10px; color: red">系统默认30天自动清理</span> -->
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { FormRules } from 'element-plus'
import * as metaApi from '@/api/meta'
import * as RecycleApi from '@/api/recycle'

defineOptions({ name: 'metaClearRecycleForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  metaName: '',
  metaId: '',
  days: undefined
})

/** 天数设置 */
const options = [
  {
    value: '7天',
    label: '7'
  },
  {
    value: '15天',
    label: '15'
  },
  {
    value: '30天',
    label: '30'
  }
]

/** 到期时间更变取值 */
const handleOptionChange = async (value) => {
  console.log(value)
}

const formRules = reactive<FormRules>({
  days: [{ required: true, message: '天数不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref
/** 打开弹窗 */
const open = async (title: string, id?: number) => {
  formLoading.value = true
  dialogVisible.value = true
  dialogTitle.value = title + ' - 设置定期清理天数'
  resetForm()
  try {
    RecycleApi.recycleDays(id).then((res) => {
      console.log(res)
      if (res) {
        formData.value = res
      } else {
        metaApi.selectForm(id).then((res) => {
          console.log(res)
          formData.value.metaName = res.metaName
          formData.value.metaId = id
        })
      }
    })
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  // 提交请求
  try {
    dialogVisible.value = false
    RecycleApi.save(formData.value).then((res) => {
      message.success('保存成功！')
      resetForm()
    })
    dialogVisible.value = false
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    metaName: '',
    metaId: '',
    days: undefined
  }
  formRef.value?.resetFields()
}
</script>
<style scoped lang="scss">
.dialog-footer {
  text-align: center;
}
</style>
